﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Modules.CMS.Repositories;
using Kaiser.Modules.CMS.Models;
using Kaiser.Models;
using System.Collections;

namespace Kaiser.Repositories.iBatis
{
    public class CMSRepositories : ICMSRepositories
    {
        private IBatisHepler IBatisHepler = null;
        public CMSRepositories()
        {
            IBatisHepler = new BatisHepler("SQLMap.CMS.config");
        }

        #region 分类管理

        public virtual DataGrid<CategoryInput> GetCategoryAll(CategorySearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<CategoryInput>();

            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("GetCategoryAll", search, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }


        public virtual void InsertCategory(CategoryInput model)
        {
            var result = IBatisHepler.ExcuteInsert("InsertCategory", model);
        }

        public virtual void UpdateCategory(CategoryInput model)
        {
            var result = IBatisHepler.ExcuteUpdate("UpdateCategory", model);
        }


        public int DeleteCategory(string id)
        {
            var result = IBatisHepler.ExcuteDelete("DeleteCategory", id);
            return result;
        }

        public int DeleteCategoryList(string ids)
        {
            Hashtable parms = new Hashtable();
            parms["IdList"] = ids.Split(',');
            var result = IBatisHepler.ExcuteDelete("DeleteCategoryList", parms);
            return result;
        }

        public CategoryInput GetCategory(string Id)
        {
            var result = IBatisHepler.ExcuteSelectObj("GetCategory", Id) as CategoryInput;
            return result;
        }

        #endregion



        #region 内容管理

        public virtual DataGrid<ContentInput> QueryContent(CategorySearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<ContentInput>();

            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("QueryContent", search, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }

        /// <summary>
        /// 保持内容，同时新增关系表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="CategoryId"></param>
        public virtual void InsertContent(ContentInput model, IList<CategoryContentInput> cateModel)
        {
            var result = IBatisHepler.ExcuteInsert("InsertContent", model);
            foreach (CategoryContentInput item in cateModel)
            {
                CategoryContentInput mod = item;
                mod.ContentId = Convert.ToInt32(result);
                var result2 = IBatisHepler.ExcuteInsert("InsertCategoryContent", item);
            }
        }

        /// <summary>
        /// 修改内容，同时修改关系表
        /// </summary>
        /// <param name="model"></param>
        public virtual void UpdateContent(ContentInput model, IList<CategoryContentInput> cateModel)
        {
            IBatisHepler.ExcuteDelete("ClearContentRef", model.Id);

            var result = IBatisHepler.ExcuteUpdate("UpdateContent", model);

            foreach (CategoryContentInput item in cateModel)
            {
                item.ContentId = model.Id;
                var result2 = IBatisHepler.ExcuteInsert("InsertCategoryContent", item);
            }
        }

        /// <summary>
        /// 删除内容，同时删除关系
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual void DeleteContent(string id)
        {
            var result2 = IBatisHepler.ExcuteInsert("InsertCategoryContent", id);
            var result = IBatisHepler.ExcuteDelete("DeleteContent", id);
        }

        /// <summary>
        /// 获取内容 根据内容id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual ContentInput GetContent(string id)
        {
            ContentInput result = IBatisHepler.ExcuteSelectObj("GetContentById", id) as ContentInput;
            return result;
        }

        /// <summary>
        /// 删除多条内容
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public virtual void DeleteContentList(string ids)
        {
    
            var result = IBatisHepler.ExcuteDelete("DeleteContentList", ids.Split(','));
        }

        #endregion
    }
}
